package com.windystory.wslibrary.base.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.windystory.wslibrary.R;
import com.windystory.wslibrary.util.DxDpUtil;
import com.windystory.wslibrary.ui.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Base TabLayoutActivity
 */
public abstract class BaseTabLayoutActivity extends BaseActivity {
    private TabLayout tableLayout;
    private ViewPager vp;
    //    private FrameLayout tablayout_height;
    private ViewPageAdapter adapter;
    private List<String> title = new ArrayList<>();
    private List<Fragment> fragmentLists = new ArrayList<>();

    @Override
    protected void initListener() {

    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_base_tablyout;
    }

    @Override
    protected void initView() {
        tableLayout = (TabLayout) findViewById(R.id.tablayout);
        vp = (ViewPager) findViewById(R.id.viewpager);
    }

    @Override
    protected void initData() {
        title.addAll(initTitles());
        fragmentLists.addAll(initFragments());
        adapter = new ViewPageAdapter(getSupportFragmentManager(), title, fragmentLists);
        vp.setAdapter(adapter);
        tableLayout.setupWithViewPager(vp);
        tableLayout.setTabsFromPagerAdapter(adapter);
        tableLayout.setBackgroundColor(initTableLayoutBGColor());

        tableLayout.setNeedSwitchAnimation(false);
        tableLayout.setSelectedTabIndicatorColor(initIndicatorColor());
        tableLayout.setSelectedTabIndicatorWidth(DxDpUtil.dp2px(this, 72));
        tableLayout.setSelectedTabIndicatorHeight(DxDpUtil.px2dp(this, 20));
        tableLayout.setTabTextColors(initTableLayoutTextNormalColor(), initTableLayoutTextSelectedColor());


        initDataInfo();
    }

    /**
     * tablayout的宽度
     */
    protected abstract float getTabLayoutSize();

    /**
     * 初始化数据信息
     */
    protected abstract void initDataInfo();

    /**
     * 指示器颜色
     * getResources().getColor(R.color.X)
     *
     * @return
     */
    protected abstract int initIndicatorColor();

    /**
     * 初始化TableLayout背景颜色
     * getResources().getColor(R.color.X)
     *
     * @return
     */
    protected abstract int initTableLayoutBGColor();

    /**
     * 选中字体颜色
     * getResources().getColor(R.color.X)
     *
     * @return
     */
    protected abstract int initTableLayoutTextSelectedColor();

    /**
     * 未选中字体颜色
     * getResources().getColor(R.color.X)
     *
     * @return
     */
    protected abstract int initTableLayoutTextNormalColor();

    /**
     * 初始化Fragment
     *
     * @return
     */
    protected abstract List<Fragment> initFragments();

    /**
     * 初始化标题
     *
     * @return
     */
    protected abstract List<String> initTitles();


    /**
     * ViewPageAdapter适配器
     */
    public class ViewPageAdapter extends FragmentPagerAdapter {
        private List<String> titleList;
        private List<Fragment> fragmentList;

        public ViewPageAdapter(FragmentManager fm, List<String> titleList, List<Fragment> fragmentList) {
            super(fm);
            this.titleList = titleList;
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }
    }


}
